<?php

namespace App\Admin\Forms\Mp;

use App\Lib\WeApp\WeApp;
use App\Wen\Utils\Settings;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class FreePublishForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        Settings::set('app_mp_free_publish_title', $input['app_mp_free_publish_title'], true);
        Settings::set('app_mp_free_publish_author', $input['app_mp_free_publish_author'], true);
        Settings::set('app_mp_free_publish_head', $input['app_mp_free_publish_head'], true);
        Settings::set('app_mp_free_publish_default_bg', $input['app_mp_free_publish_default_bg'], true);
        Settings::set('app_mp_free_publish_tail', $input['app_mp_free_publish_tail'], true);
        Settings::set('app_mp_free_publish_thumb_url', $input['app_mp_free_publish_thumb_url'], true);
        Settings::set('app_mp_free_publish_is_wxurl', _empty_($input['app_mp_free_publish_is_wxurl']) ? 0 : 1, true);
        Settings::set('app_mp_free_publish_need_open_comment', _empty_($input['app_mp_free_publish_need_open_comment']) ? 0 : 1, true);
        Settings::set('app_mp_free_publish_only_fans_can_comment', _empty_($input['app_mp_free_publish_only_fans_can_comment']) ? 0 : 1, true);
        Settings::get('app_mp_free_publish_post_bg_color', $input['app_mp_free_publish_post_bg_color']);

        $weapp = new WeApp('mp');
        $mp = $weapp->getMpServicer();
        if($input['app_mp_free_publish_default_bg']){
            $res_bg = $mp->uploadMediaImg($input['app_mp_free_publish_default_bg']);
            if($res_bg['code'] == 200){
                Settings::set('app_mp_free_publish_default_bg_mp', $res_bg['url'], true);
            }
        }


        return $this->response()->success('保存成功')->refresh();
    }

    public function form()
    {

        $this->textarea('app_mp_free_publish_title', '推文标题')->help('【the_number】推文期数，系统自动维护，分站间独立，【first_title】第一篇笔记的标题<br>默认为：墙墙第【the_number】期，【first_title】...')->default(Settings::get('app_mp_free_publish_title', '墙墙第【the_number】期，【first_title】...'))->required();
        $this->text('app_mp_free_publish_author', '作者')->default(Settings::get('app_mp_free_publish_author', ''));
        $this->image('app_mp_free_publish_thumb_url', '封面图')->url('files/uploads')->uniqueName()->autoUpload()->required()->default(Settings::get('app_mp_free_publish_thumb_url','https://img.mini.minisns.cn/2023/05/22/17f31da7ce65a148b7c879c066f56f71.png') );
        $this->switch('app_mp_free_publish_is_wxurl', '小程序链接')->default(Settings::get('app_mp_free_publish_is_wxurl', 0));
        $this->switch('app_mp_free_publish_need_open_comment', '打开评论')->default(Settings::get('app_mp_free_publish_need_open_comment', 1));
        $this->switch('app_mp_free_publish_only_fans_can_comment', '仅粉丝能评论')->default(Settings::get('app_mp_free_publish_only_fans_can_comment', 0));
        if(__system_is_model_enable('laradocs', 'dcat-neditor')){
            $this->neditor('app_mp_free_publish_head', '推文头部')->default(Settings::get('app_mp_free_publish_head', ''));
        }else{
            $this->editor('app_mp_free_publish_head', '推文头部')->default(Settings::get('app_mp_free_publish_head', ''));
        }
        $this->image('app_mp_free_publish_default_bg', '背景图片')->url('files/uploads')->uniqueName()->autoUpload()->required()->default(Settings::get('app_mp_free_publish_default_bg', env('APP_URL', 'https://mini.minisns.cn').'/statics/images/Mp/640.png') );
        $this->color('app_mp_free_publish_post_bg_color', '笔记背景颜色')->rgba()->default(Settings::get('app_mp_free_publish_post_bg_color', 'rgba(108,130,250,0.5)'))->required();
        if(__system_is_model_enable('laradocs', 'dcat-neditor')){
            $this->neditor('app_mp_free_publish_tail', '推文尾部')->default(Settings::get('app_mp_free_publish_tail', '<section style="background: rgba(0, 0, 0, 0.4);padding: 4px;text-align: center;color: rgba(255, 255, 255, 0.8);font-size: 12px;">以上内容由用户发布，不代表本公众号立场</section>'));
        }else{
            $this->editor('app_mp_free_publish_tail', '推文尾部')->default(Settings::get('app_mp_free_publish_tail', '<section style="background: rgba(0, 0, 0, 0.4);padding: 4px;text-align: center;color: rgba(255, 255, 255, 0.8);font-size: 12px;">以上内容由用户发布，不代表本公众号立场</section>'));
        }

    }
}
